﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using AMG.DALProfile;

namespace AMG.Web
{
    public partial class reg : AMG.Web.UI.Page.UserPage
    {
        private int bookno;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownList(ddlRound, "SELECT   AMG_Round_Company.RoundID, AMG_Round.RoundName, AMG_Round.BookNo FROM AMG_Round_Company INNER JOIN  AMG_Round ON AMG_Round_Company.RoundID = AMG_Round.ID INNER JOIN AMG_Proxy ON AMG_Round_Company.CompanyID = AMG_Proxy.ID where AMG_Proxy.ID=" + UserID);
            }
        }

        private void doreg()
        {
            try
            {
                AMG.Web.UI.Page.BasePage bp = new AMG.Web.UI.Page.BasePage();
                int UserID = bp.GetUserID();
                AMG.Model.AMG_UserInfo usinfo = new AMG.Model.AMG_UserInfo();
                AMG.Model.AMG_Proxy apinfo = new AMG.Model.AMG_Proxy();
                usinfo.UserName = Request.Form["name"];
                if (sex1.Checked  == true)
                {
                    usinfo.Sex = false;
                }
                else if (sex2.Checked  == true)
                {
                    usinfo.Sex = true;
                }
                usinfo.City = "";
                usinfo.CustomerProperty="1";
                usinfo.Company = company.Value;
                usinfo.Positison = position .Value;
                usinfo.CRNo = crno.Value;
                if (driverlisecen2.Checked==true)
                {
                    usinfo.DriverLisecen = false;
                }
                else if (driverlisecen1.Checked ==true)
                {
                    usinfo.DriverLisecen = true;
                }
                usinfo.Phone = phone.Value;
                usinfo.Address =address.Value;
                usinfo.ZipCode =zipcode.Value;
                usinfo.Mail =mail.Value;
                if (isbook2 .Checked==true )
                {
                    usinfo.IsBook = false;
                }
                else if (isbook1.Checked ==true)
                {
                    usinfo.IsBook = true;
                }
                usinfo.CarNo = int.Parse(carno.Value);
                usinfo.ArrivalDate = DateTime.Today;
                usinfo.ArrivalTraffic = 0;            
                usinfo.TrafficNo = "";
                usinfo.StartH = "";
                usinfo.StartM = "";
                usinfo.EndH = "";
                usinfo.EndM = "";
                usinfo.StartCity = "";
                usinfo.NeedGet = false;
                usinfo.ReturnDate = DateTime.Today ;
                usinfo.ReturnTraffic = 0;
                usinfo.TrafficNo1 = "";
                usinfo.ReturnH = "";
                usinfo.ReturnM = "";
                usinfo.ReturnCity = "";
                usinfo.NeedSend = false;
                usinfo.Room = false;
                usinfo.Golf = false;
                usinfo.Equipment = false;
                usinfo.GolfTime = 0;
                usinfo.RoundNo = int.Parse(ddlRound.SelectedValue);
                                
                usinfo.CompanyID = UserID;
                AMG.DALSQLServer.AMG_Proxy ap = new AMG.DALSQLServer.AMG_Proxy();
                apinfo.Code = ap.GetModel(UserID).Code;

                object obj = DbHelper.ExecuteScalar(CommandType.Text, "select  TOP 1 RIGHT(AMGCode, 2) from AMG_UserInfo where CompanyID='" + UserID + "' order by ID DESC", null);
                string code;
                if (obj == null)
                {
                    code = "0";
                }
                else
                {
                    code = obj.ToString();
                }
                usinfo.AMGCode = apinfo.Code + (int.Parse(code) + 1).ToString("00");
                AMG.DALSQLServer.AMG_UserInfo us = new AMG.DALSQLServer.AMG_UserInfo();
                us.Add(usinfo);
                int userid = int.Parse(DbHelper.ExecuteScalar(CommandType.Text, "select ID from AMG_UserInfo where AMGCode ='" + usinfo.AMGCode + "'", null).ToString());
                if (isbook1.Checked==true)
                {
                    AMG.Model.AMG_Book bookinfo = new AMG.Model.AMG_Book();
                    AMG.DALSQLServer.AMG_Book ab = new AMG.DALSQLServer.AMG_Book();
                    if (C63.Checked ==true)
                    {
                        bookinfo.CarID = 1;
                        bookinfo.UserID = userid;
                        ab.Add(bookinfo);
                    }
                    if (SLK55 .Checked ==true)
                    {
                        bookinfo.CarID = 2;
                        bookinfo.UserID = userid;
                        ab.Add(bookinfo);
                    }
                    if (SL63.Checked==true)
                    {
                        bookinfo.CarID = 3;
                        bookinfo.UserID = userid;
                        ab.Add(bookinfo);
                    }
                    if (CLS63.Checked == true)
                    {
                        bookinfo.CarID = 5;
                        bookinfo.UserID = userid;
                        ab.Add(bookinfo);
                    }
                    if (G55.Checked == true)
                    {                        
                        bookinfo.CarID = 6;
                        bookinfo.UserID = userid;
                        ab.Add(bookinfo);
                    }
                    if (ML63.Checked == true)
                    {
                        bookinfo.CarID = 7;
                        bookinfo.UserID = userid;
                        ab.Add(bookinfo);
                    }
                    if (S65.Checked == true)
                    {
                        bookinfo.CarID = 4;
                        bookinfo.UserID = userid;
                        ab.Add(bookinfo);
                    }

                }
                if (int.Parse (carno.Value)>0)
                {
                    cars(userid,int.Parse (carno.Value));
                }
            }
            catch(Exception ex)
            {
                ex.ToString();
                Response.Redirect("regfail.aspx", false);
            }
            Response.Write("<script>location.href=\"regok.aspx\"</script>");
        }

        private void cars(int userid,int carno)
        {
            AMG.Model.AMG_Cars carsinfo = new AMG.Model.AMG_Cars();
            AMG.DALSQLServer.AMG_Cars ac = new AMG.DALSQLServer.AMG_Cars();
            for (int i = 1; i <= carno; i++)
            {
                carsinfo.CarBland = Request.Form["carbland" + i];
                carsinfo.CarName = Request.Form["carname" + i];
                carsinfo.UserID = userid;
                ac.Add(carsinfo);
            }
        }

        //private void cars(int userid)
        //{
        //    AMG.Model.AMG_Cars carsinfo = new AMG.Model.AMG_Cars();
        //    AMG.DALSQLServer.AMG_Cars ac = new AMG.DALSQLServer.AMG_Cars();
        //    if (Request.Form["carbland1"] != null)
        //    {                
        //        carsinfo.CarBland =carbland1.Value;
        //        carsinfo.CarName = carname1.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland2"] != null)
        //    {
        //        carsinfo.CarBland = carbland2.Value;
        //        carsinfo.CarName = carname2.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland3"] != null)
        //    {
        //        carsinfo.CarBland = carbland3.Value;
        //        carsinfo.CarName = carname3.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland4"] != null)
        //    {
        //        carsinfo.CarBland = carbland4.Value;
        //        carsinfo.CarName = carname4.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland5"] != null)
        //    {
        //        carsinfo.CarBland = carbland5.Value;
        //        carsinfo.CarName =carname4.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland6"] != null)
        //    {
        //        carsinfo.CarBland = carbland6.Value;
        //        carsinfo.CarName = carname6.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland7"] != null)
        //    {
        //        carsinfo.CarBland = carbland7.Value ;
        //        carsinfo.CarName = carname7.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland8"] != null)
        //    {
        //        carsinfo.CarBland = carbland8.Value;
        //        carsinfo.CarName = carname8.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland9"] != null)
        //    {
        //        carsinfo.CarBland = carbland9.Value;
        //        carsinfo.CarName = carname9.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //    if (Request.Form["carbland10"] != null)
        //    {
        //        carsinfo.CarBland = carbland10.Value;
        //        carsinfo.CarName =carname10.Value;
        //        carsinfo.UserID = userid;
        //        ac.Add(carsinfo);
        //    }
        //}

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            doreg();
        }

        public static void BindDropDownList(System.Web.UI.WebControls.DropDownList ddl, string sql)
        {
            if (sql == null || sql == "")
            {
                ddl.Items.Clear();
                return;
            }

            DataTable dt = DbHelper.ExecuteTable(CommandType.Text, sql, null);
            ddl.DataSource = dt;
            ddl.DataTextField = dt.Columns[1].ColumnName;
            ddl.DataValueField = dt.Columns[0].ColumnName;
            ddl.SelectedIndex = 0;
            ddl.SelectedValue = null;
            ddl.DataBind();
        }
    }
}
